﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace PFMConfigurations
{
    public class ConfigurationManager<T>
    {
        protected string File { get; set; }
        public List<T> Items { get; set; }
        /// <summary>
        /// Items的总数
        /// </summary>
        /// <returns>Items.Count</returns>
        public int getCount()
        {
            return Items!=null? Items.Count:0;
        }
        /// <summary>
        /// 验证有无此id
        /// </summary>
        /// <param name="id">需要验证的id</param>
        /// <returns>有则返回id，无则返回null</returns>
        public virtual T getItem(object id)
        {
            return default(T);
        }
        public bool Save(string name)
        {
            string b = name;
                using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<T>));
                    xmlSerializer.Serialize(stringWriter,Items);

                    FileStream fs = new FileStream(b+"实验用数据.xml", FileMode.OpenOrCreate);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(stringWriter.ToString());
                    sw.Close();
                    fs.Close();
                    //ageBox.Show("写入文件成功！");  
                }
                return true;

        }
        
    }
}
